﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ClientLib;
using JigsawAdmin.SubForms.Security;

namespace JigsawAdmin
{
    public partial class Main : Form
    {
        ClientUtil _cutil;
        JsvcEnc.EncSoapXml xmlsvc;

        // declare all subforms
        Home frmHome;
        Users frmUsers;
        Roles frmRoles;
        API frmAPI;
        Groups frmGroups;

        public Main(ClientUtil cUtil, string connected)
        {
            InitializeComponent();

            try
            {
                _cutil = cUtil;
                connectedStatusLabel.Text = connected;
                userStatusLabel.Text = _cutil.UserName;

                // initialize all subforms and set as MDI children of main form
                frmGroups = new Groups(_cutil);
                frmGroups.MdiParent = this;
                frmGroups.Show();
                frmGroups.WindowState = FormWindowState.Maximized;

                frmAPI = new API(_cutil);
                frmAPI.MdiParent = this;
                frmAPI.Show();
                frmAPI.WindowState = FormWindowState.Maximized;

                frmRoles = new Roles(_cutil);
                frmRoles.MdiParent = this;
                frmRoles.Show();
                frmRoles.WindowState = FormWindowState.Maximized;

                frmUsers = new Users(_cutil);
                frmUsers.MdiParent = this;
                frmUsers.Show();
                frmUsers.WindowState = FormWindowState.Maximized;

                frmHome = new Home(_cutil);
                frmHome.MdiParent = this;
                frmHome.Show();
                frmHome.WindowState = FormWindowState.Maximized;
                frmHome.HomeUpdated += new Home.HomeUpdatedEventHandler(frmHome_HomeUpdated);
            }
            catch (Exception ex)
            {
                ErrorUtil eutil = new ErrorUtil();
                eutil.LogException(_cutil.UserID, "Main Main", "", ex);
            }
        }

        /// <summary>
        /// Used to trigger a resize of the main form, which helps the MDI forms correctly
        /// maximize within the main form when initially displayed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void frmHome_HomeUpdated(object sender, HomeUpdateEventArgs e)
        {
            this.Width += 1;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Main_Shown(object sender, EventArgs e)
        {
            
        }

        // ********************************************************
        // System
        // ********************************************************
        private void homeMenuItem_Click(object sender, EventArgs e)
        {
            frmHome.Focus();
        }

        private void closeMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        // ********************************************************
        // Security
        // ********************************************************
        private void usersMenuItem_Click(object sender, EventArgs e)
        {
            frmUsers.Focus();
        }

        private void rolesMenuItem_Click(object sender, EventArgs e)
        {
            frmRoles.Focus();
        }

        private void apiMenuItem_Click(object sender, EventArgs e)
        {
            frmAPI.Focus();
        }

        private void groupsMenuItem_Click(object sender, EventArgs e)
        {
            frmGroups.Focus();
        }

        

        // ********************************************************
        // Replication
        // ********************************************************



        // ********************************************************
        // Help
        // ********************************************************

        private void indexMenuItem_Click(object sender, EventArgs e)
        {
            AdminHelp ahelp = new AdminHelp(_cutil);
            ahelp.Show();
        }
    }
}
